package com.common.filters;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import com.common.util.ParameterRequestWrapper;

public class UrlFilter  implements Filter {
	
	@Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        
    	HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponseWrapper httpResponse = new HttpServletResponseWrapper((HttpServletResponse) response);
        System.out.println(httpRequest.getRequestURI());
        String path=httpRequest.getRequestURI();
        
        String apiName = httpRequest.getParameter("apiName");
        if(null != apiName)
        {
        	ParameterRequestWrapper wrapper = new ParameterRequestWrapper(httpRequest);
        	wrapper.removeParameter("apiName");
            String newPath = path + apiName;
            httpRequest.getRequestDispatcher(newPath).forward(wrapper,response);
        }
        else
        {
        	chain.doFilter(request,response);
        }
        
        return;
    }

}
